package com.foo.ReadDataTest;

import java.io.File;
import java.util.Calendar;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;

import junit.framework.Assert;

import org.junit.Test;

import com.foo.Constants.Constants;
import com.foo.DataProvider.*;
import com.foo.DataReciever.DataReciever;
import com.foo.Writer.*;

public class ReadDataTest 
{
	@Test
	public void readdatatest()
	{
		ProviderRemote dataprovider = null;

		try
		{
			Properties properties = new Properties();
			properties.put(Context.PROVIDER_URL, "jnp://localhost:1099");
			properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
			properties.put(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
			Context ctx = new InitialContext(properties);
			Object ref = ctx.lookup("Provider/remote");
			//DataProviderRemote 
			dataprovider = (ProviderRemote) PortableRemoteObject.narrow(ref, ProviderRemote.class);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}	
			DataReciever recieve=new DataReciever();
			dataprovider.getData("Country", "Ireland",null,null,null);
			String[] test=recieve.readtickerinfodata();
			
			WriterClient writer = new WriterClient();
			String fileName =Constants.FILE_PATH + "data.csv";
			//client.generateCSV(fileName, trialstring,"Spatial");
			writer.selectfile(fileName, test,"Spatial");
			
			File f = new File(fileName);
			if(f.exists())
			{
				System.out.println("File Created");			
			}
			else
			{
				System.out.println("File Create failed");			
			}
			int lengthexpected = (test.length)/8;
			
			Assert.assertEquals(4, lengthexpected);
	}
	@Test
	public void readdatatest_1()
	{
		ProviderRemote dataprovider = null;

		try
		{
			Properties properties = new Properties();
			properties.put(Context.PROVIDER_URL, "jnp://localhost:1099");
			properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
			properties.put(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
			Context ctx = new InitialContext(properties);
			Object ref = ctx.lookup("Provider/remote");
			//DataProviderRemote 
			dataprovider = (ProviderRemote) PortableRemoteObject.narrow(ref, ProviderRemote.class);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}	
			DataReciever recieve=new DataReciever();
			
			dataprovider.getData("Country", "India", null, null, null);
			String[] test=recieve.readtickerinfodata();
			//int lengthexpected = (test.length)/8;
			
			Assert.assertEquals(1, test.length);
	}
	@Test
	public void readdatatest_2()
	{
		ProviderRemote dataprovider = null;

		try
		{
			Properties properties = new Properties();
			properties.put(Context.PROVIDER_URL, "jnp://localhost:1099");
			properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
			properties.put(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
			Context ctx = new InitialContext(properties);
			Object ref = ctx.lookup("Provider/remote");
			//DataProviderRemote 
			dataprovider = (ProviderRemote) PortableRemoteObject.narrow(ref, ProviderRemote.class);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}	
			DataReciever recieve=new DataReciever();
		
			java.sql.Date sqltoDate = java.sql.Date.valueOf("2009-11-20");
			java.sql.Date sqlfromDate = java.sql.Date.valueOf("2009-11-13");

			
			dataprovider.getData("GE", "OneWeek", null, sqlfromDate, sqltoDate);
			String[] test=recieve.readtickerinfodata();
			
			WriterClient writer = new WriterClient();
			String fileName =Constants.FILE_PATH + "data.csv";
			writer.selectfile(fileName, test,"Snapshot");
			
			File f = new File(fileName);
			if(f.exists())
			{
				System.out.println("File Created");			
			}
			else
			{
				System.out.println("File Create failed");			
			}			
			Assert.assertNotNull(test);
			//Assert.assertEquals(0, lengthexpected);
	}
	@Test
	public void readdatatest_3()
	{
		ProviderRemote dataprovider = null;

		try
		{
			Properties properties = new Properties();
			properties.put(Context.PROVIDER_URL, "jnp://localhost:1099");
			properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
			properties.put(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
			Context ctx = new InitialContext(properties);
			Object ref = ctx.lookup("Provider/remote");
			//DataProviderRemote 
			dataprovider = (ProviderRemote) PortableRemoteObject.narrow(ref, ProviderRemote.class);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}	
			DataReciever recieve=new DataReciever();
		
			java.util.Date date = new java.util.Date();
			java.util.Date date1 = new java.util.Date();
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date1);
			calendar.add(Calendar.DAY_OF_YEAR, -7);
			java.util.Date fromDate = calendar.getTime();
			java.sql.Date sqltoDate = new java.sql.Date(fromDate.getTime());
			java.sql.Date sqlfromDate = new java.sql.Date(date.getTime());

			
			dataprovider.getData("GE", "OneWeek", null, sqlfromDate, sqltoDate);
			String[] test=recieve.readtickerinfodata();
			int lengthexpected = (test.length)/6;
			
		    Assert.assertEquals(0, lengthexpected);
	}
	@Test
	public void readdatatest_4()
	{
		ProviderRemote dataprovider = null;

		try
		{
			Properties properties = new Properties();
			properties.put(Context.PROVIDER_URL, "jnp://localhost:1099");
			properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
			properties.put(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
			Context ctx = new InitialContext(properties);
			Object ref = ctx.lookup("Provider/remote");
			//DataProviderRemote 
			dataprovider = (ProviderRemote) PortableRemoteObject.narrow(ref, ProviderRemote.class);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}	
			DataReciever recieve=new DataReciever();
		
			java.util.Date date = new java.util.Date();
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.add(Calendar.DAY_OF_YEAR, -1000);
			java.util.Date fromDate = calendar.getTime();
			Calendar calendar1 = Calendar.getInstance();
			calendar.setTime(date);
			calendar.add(Calendar.DAY_OF_YEAR, -993);
			java.util.Date toDate = calendar1.getTime();
			java.sql.Date sqltoDate = new java.sql.Date(toDate.getTime());
			java.sql.Date sqlfromDate = new java.sql.Date(fromDate.getTime());

			dataprovider.getData("GE", null, null, sqlfromDate, sqltoDate);
			String[] test=recieve.readtickerinfodata();

			WriterClient writer = new WriterClient();
			String fileName =Constants.FILE_PATH + "data.json";
			writer.selectfile(fileName, test,"Temporal");
			
			File f = new File(fileName);
			if(f.exists())
			{
				System.out.println("File Created");			
			}
			else
			{
				System.out.println("File Create failed");			
			}			
			Assert.assertNotNull(test);
			//Assert.assertEquals(0, lengthexpected);
	}
	
	@Test
	public void readdatatest_5()
	{
		ProviderRemote dataprovider = null;

		try
		{
			Properties properties = new Properties();
			properties.put(Context.PROVIDER_URL, "jnp://localhost:1099");
			properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
			properties.put(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
			Context ctx = new InitialContext(properties);
			Object ref = ctx.lookup("Provider/remote");
			//DataProviderRemote 
			dataprovider = (ProviderRemote) PortableRemoteObject.narrow(ref, ProviderRemote.class);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}	
			DataReciever recieve=new DataReciever();
			
			java.util.Date date = new java.util.Date();
			java.util.Date date1 = new java.util.Date();
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date1);
			calendar.add(Calendar.DAY_OF_YEAR, -7);
			java.util.Date fromDate = calendar.getTime();
			java.sql.Date sqltoDate = new java.sql.Date(fromDate.getTime());
			java.sql.Date sqlfromDate = new java.sql.Date(date.getTime());


			dataprovider.getData("GE", null, null, sqlfromDate, sqltoDate);
			String[] test=recieve.readtickerinfodata();
			int lengthexpected = (test.length)/6;	
		    Assert.assertEquals(0, lengthexpected);
	}
	
	@Test
	public void readdatatest_6()
	{
		ProviderRemote dataprovider = null;
		
		try
		{
			Properties properties = new Properties();
			properties.put(Context.PROVIDER_URL, "jnp://localhost:1099");
			properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
			properties.put(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
			Context ctx = new InitialContext(properties);
			Object ref = ctx.lookup("Provider/remote");
			//DataProviderRemote 
			dataprovider = (ProviderRemote) PortableRemoteObject.narrow(ref, ProviderRemote.class);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}	
			DataReciever recieve=new DataReciever();
			
			dataprovider.getData("TopVolume",null,null,null,null);
			String[] test=recieve.readtickerinfodata();

			WriterClient writer = new WriterClient();
			String fileName =Constants.FILE_PATH + "data.xml";
			writer.selectfile(fileName, test,"Temporal");
			
			File f = new File(fileName);
			if(f.exists())
			{
				System.out.println("File Created");			
			}
			else
			{
				System.out.println("File Create failed");			
			}			
			Assert.assertNotNull(test);
	}
	@Test
	public void readdatatest_7()
	{
ProviderRemote dataprovider = null;
		
		try
		{
			Properties properties = new Properties();
			properties.put(Context.PROVIDER_URL, "jnp://localhost:1099");
			properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
			properties.put(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
			Context ctx = new InitialContext(properties);
			Object ref = ctx.lookup("Provider/remote");
			//DataProviderRemote 
			dataprovider = (ProviderRemote) PortableRemoteObject.narrow(ref, ProviderRemote.class);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}	
			DataReciever recieve=new DataReciever();
			
			dataprovider.getData("ABC",null,null,null,null);
			String[] test=recieve.readtickerinfodata();
			int lengthexpected = (test.length)/6;	
		    Assert.assertEquals(0, lengthexpected);
	}	
}
